package com.example.canavar;

import android.app.ActionBar.LayoutParams;
import android.app.Fragment;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.text.format.Time;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.utils.SearchResult;
import com.example.utils.SearchResultManager;
import com.example.utils.Utils;

public class CarouselFragment extends Fragment {

	private static final float INITIAL_ITEMS_COUNT = 3.5F;
	private LinearLayout mCarouselContainer;
	TypedArray boxResourcesTypedArray;
	int boxWidth;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater
				.inflate(R.layout.carousel_layout, container, false);

		// Get reference to carousel container
		mCarouselContainer = (LinearLayout) view.findViewById(R.id.carousel);

		// Compute the width of a carousel item based on the screen width and
		// number of initial items.
		final DisplayMetrics displayMetrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay()
				.getMetrics(displayMetrics);
		boxWidth = (int) (displayMetrics.widthPixels / INITIAL_ITEMS_COUNT);
		boxResourcesTypedArray = getResources().obtainTypedArray(
				R.array.boxes_array);

		if (SearchResultManager.getInstance().getResults() != null) {
			// Populate the carousel with items

			Utils.Log("onCreateView , results count "
					+ SearchResultManager.getInstance().getResults().size());
			for (SearchResult res : SearchResultManager.getInstance()
					.getResults()) {
				// Utils.Log("onCreateView is adding view " + res.getBrand() +
				// " in " + res.getShop());
				addNewBox(inflater, res);
			}
		}

		return view;
	}

	void addNewBox(LayoutInflater inflater, SearchResult res) {
		int randomBox = boxResourcesTypedArray.getResourceId(
				mCarouselContainer.getChildCount()
						% boxResourcesTypedArray.length(), -1);
		LinearLayout item = (LinearLayout) inflater.inflate(
				R.layout.carousel_item, null);

		item.setBackgroundResource(randomBox);

		TextView text = (TextView) item.getChildAt(0);
		text.setText(res.getBrand());
		TextView shop = (TextView) item.getChildAt(1);
		shop.setText(res.getShop());
		TextView time = (TextView) item.getChildAt(2);
		time.setText(getDayStr(res.getDay()));

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				boxWidth, LayoutParams.MATCH_PARENT);
		item.setLayoutParams(params);

		mCarouselContainer.addView(item, 0);
	}

	String getDayStr(int day) {
		int diffWithToday = Utils.getDiffWithToday(day);
		switch (diffWithToday) {
		case 0:
			return getString(R.string.today);
		case 1:
			return getString(R.string.yesterday);
		default:
			return getString(R.string.this_week);
		}
	}
}